package com.sztle.admodule.apiprocessor;

import android.app.Activity;
import android.view.ViewGroup;

import com.sztle.admodule.GmAdManager;
import com.sztle.admodule.IShowListener;
import com.sztle.admodule.bean.AdReportInfo;
import com.sztle.admodule.handler.SplashHandler;
import com.sztle.admodule.apiprocessor.listener.IStartProgress;
import com.sztle.admodule.someutils.ThreadUtils;
import com.sztle.admodule.control.AppInfoManager;
import com.sztle.admodule.control.event.SzAdEvents;
import com.sztle.admodule.control.event.SzTrackEvents;
import com.sztle.admodule.control.manager.ControlManager;
import com.sztle.admodule.control.utils.UserDeviceUtils;

import java.util.Random;

public class OutSplashAdApi {

    private OutSplashAdApi() {
    }

    static class Holder {
        static OutSplashAdApi processor = new OutSplashAdApi();
    }

    public static OutSplashAdApi getInstance() {
        return Holder.processor;
    }


    private IStartProgress splashListener;


    public void checkSplashAd(Activity activity, ViewGroup adContainer, IStartProgress listener) {
        splashListener = listener;
        if (!GmAdManager.isUseAdExp() && ControlManager.isFinalAdjustOpen()) {
            ThreadUtils.runOnUiThreadDelayed(new Runnable() {
                @Override
                public void run() {
                    if (splashListener != null) {
                        splashListener.cancelLoadingProgress();
                    }
                }
            }, 100);
            return;
        }
        if (!GmAdManager.isAdSdkReady()) {
            return;
        }
        if(!checkSecondSplash){
            startFirstLoadAndShowSplashV2(activity, adContainer);
        } else {
            startSecondLoadAndShowSplashV2(activity,adContainer);
        }


    }

    public boolean isLoadEnd() {
        if (!GmAdManager.isUseAdExp()) {
            return true;
        }
        if (!GmAdManager.isAdSdkReady()) {
            return true;
        }
        return !isLoading;
    }

    public void setLoadEnd() {
        isLoading = false;
    }

    private boolean isLoading = false;
    private boolean isStartLoad = false;

    private boolean checkSecondSplash = false;//是否需要检查第二次开屏

    private int firstIndex = 0;

    private void startFirstLoadAndShowSplashV2(Activity activity, ViewGroup adContainer) {
        if (isStartLoad) {
            return;
        }
        if(!GmAdManager.isLocalAdExp()){
            return;
        }
        if (activity == null || activity.isDestroyed()) {
            isLoading = false;
            return;
        }
        isStartLoad = true;
        isLoading = true;
        Random random = new Random();
        firstIndex = random.nextInt(2);

        SplashHandler.loadAndShow(firstIndex, activity, adContainer, new IShowListener() {

            @Override
            public void onFinalEnd(boolean result) {
                if (!result) {
                    isLoading = false;
                    if (splashListener != null) {
                        splashListener.cancelLoadingProgress();
                    }
                    return;
                }
                if(UserDeviceUtils.isGMore()){
                    isLoading = false;
                    if (splashListener != null) {
                        splashListener.cancelLoadingProgress();
                    }
                    return;
                }
                checkSecondSplash = true;
                if (splashListener != null) {
                    splashListener.resumeLoadingProgress();
                }
                startSecondLoadAndShowSplashV2(activity, adContainer);
            }

            @Override
            public void onAdShow(String ecpm) {
                if (splashListener != null) {
                    splashListener.pauseLoadingProgress();
                }
                ControlManager.reportAppLog(SzTrackEvents.splash1_show);
                ControlManager.reportAdLog(SzAdEvents.show_splash_1, new AdReportInfo(ecpm, "page_loading"));
            }

        });

    }

    private void startSecondLoadAndShowSplashV2(Activity activity, ViewGroup adContainer) {
        if (activity == null || activity.isDestroyed()) {
            isLoading = false;
            return;
        }
        ControlManager.triggerQueryAdjust();
        if(!GmAdManager.isUseAdExp()){
            if(!SplashHandler.isShowing() && ControlManager.isFinalAdjustOpen()){
                isLoading = false;
                checkSecondSplash = false;
                if (splashListener!= null) {
                    splashListener.cancelLoadingProgress();
                }
            }
            return;
        }
        if (AppInfoManager.isHomeActivityExist()) {
            //热启动
            if (ControlManager.isSingleHotSplash()) {
                isLoading = false;
                checkSecondSplash = false;
                if (splashListener != null) {
                    splashListener.cancelLoadingProgress();
                }
                return;
            }
        } else {
            if (ControlManager.isSingleSplash()) {
                isLoading = false;
                checkSecondSplash = false;
                if (splashListener != null) {
                    splashListener.cancelLoadingProgress();
                }
                return;
            }
        }

        if(!checkSecondSplash){
            //加载过了，不再进入第二次开屏
            return;
        }
        checkSecondSplash = false;
        int curIndex = ++firstIndex % 2;
        SplashHandler.loadAndShow(curIndex, activity, adContainer, new IShowListener() {

            @Override
            public void onFinalEnd(boolean result) {
                isLoading = false;
                if (splashListener != null) {
                    splashListener.cancelLoadingProgress();
                }
            }

            @Override
            public void onAdShow(String ecpm) {
                if (splashListener != null) {
                    splashListener.pauseLoadingProgress();
                }
                ControlManager.reportAppLog(SzTrackEvents.splash2_show);
                ControlManager.reportAdLog(SzAdEvents.show_splash_2, new AdReportInfo(ecpm, "page_loading"));
            }

        });

    }


    public void resetAdFlag() {
        isStartLoad = false;
        isLoading = false;

        firstIndex = 0;
        SplashHandler.setClearFlag();
    }

}
